package lschgcdev.net;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;

public class CSHost extends NetBasicIO{

	public LinkedList<CSUser> userConnected = new LinkedList<CSUser>();
	public DataNet dataNet;
	protected ServerSocket servS;
	
	public CSHost(String ID, HostType hostType){
		super(ID);
		dataNet = hostType.get();
	}
	
	public void open(int port) throws IOException{
		log.notice("Server initialize on port: "+port);
		dataNet.inetSA = new InetSocketAddress(port);
		
		log.notice("Open port...");
		servS = new ServerSocket(port);
		
		log.notice("Listening...");
		dataNet.attempt(this);
	}
	
	public boolean accept(){
		try{
			log.notice("Attemping to connection...");
			Socket socket = servS.accept();
			
			log.notice("User connected! "+socket.getInetAddress().getHostAddress());
			CSUser newUser = new CSUser(socket);
			return userConnected.add(newUser);
		}catch(IOException ex){
			return false;
		}
	}
	
	
}
